<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/11 0011
 * Time: 13:38
 */

namespace app\index\controller;

use think\cache\driver\Redis;
use \think\facade\Config;
use think\Controller;

class Chapter extends Controller
{
    public function index(){
        $book_id = input("book_id");
        if(empty($book_id)) return "";
        return $book_id;
        return view("index");
    }
    //章节阅读
    public function read(){
        $book_id = input('book_id');
        $book = db("book")->where('id',$book_id)->where('state',1)->find();
        if(empty($book)){
            $this->error('该书籍不存在','/');
        }
        $chapter_index = input('chapter_index');
        if(empty($chapter_index)){
            $chapter = db("chapter")->where('state',1)->order("index asc")->where('book_id',$book_id)->find();
        }else{
            $chapter = db("chapter")->where('state',1)->where('book_id',$book_id)->where('index',$chapter_index)->find();
        }
        $this->chapter_pv($chapter,$book);
        if(empty($chapter)){
            $end_index = $chapter_index;
//            $this->error('已经是最后一章了',"/index/chapter/read/book_id/{$book_id}/chapter_index/{$end_index}");
            $this->error('抱歉没有更多章节');
        }
        $content = db("content")->where('id',$chapter['content_id'])->find();
        $this->assign([
            'book' => $book,
            'chapter' => $chapter,
            'content' => $content,
        ]);
        // 判断该用户类型是都是本网站的免VIP用户
        $m_id = session('member_id');
        $member = db("member")->where('id',$m_id)->find();
//        if($member['type'] == -1)
        //判断是否收费
        if($chapter['is_free'] > 0 || $member['type'] != -1){ //收费
            //判断是否购买过该书籍
            $isBuy = $this->isBuy($chapter,$book);
            if($isBuy) return view("read");

//            判断是否是自动购买
            //根据章节字数计算出需要多少书币
            $money = floor($chapter['size']/Config::get('custom.money_words'));
            if($member['money']<$money){
                return "书币不足 请先充值 跳转到充值界面";
            }
            //扣除用户书币
            db("member")->where('id',$m_id)->setDec('money',$money);
            $autoBuy = $this->autoBuy($m_id,$chapter['id'],$money);
            if($autoBuy) return view("read");

            $this->assign([
                'money' => $money
            ]);
            return view("vip_read");
        }
        return view("read");
    }
    public function isBuy($chapter,$book){
        $isBuy = db("buy")
            ->where('m_id',$book['m_id'])
            ->where('c_id',$chapter['id'])->find();
        if(empty($isBuy)){
            return false;//没有购买
        }else{
            return true;//已经购买
        }
    }
    //自动购买章节
    public function autoBuy($m_id,$chapter_id,$money){
        $data = [
            'm_id'=>$m_id,
            'c_id'=>$chapter_id,
            'money' =>$money
        ];
        $res = db('buy')->insert($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
//    手动购买章节
    public function chapterBuy(){
        $param = request()->param();
        $m_id = session('member_id');
        $chapter = db("chapter")->where('state',1)->where('id',$param['chapter_id'])->find();
        $money = floor($chapter['size']/Config::get('custom.money_words'));
        if(empty($m_id)){
            return ['state' => -1,'msg' => '请先登陆'];
        }
        $auto = $param['is_check']?1:0;
        db("member")->where("id",$m_id)->update(['is_auto' => $auto]);
        $data = [
            'm_id'=>$m_id,
            'c_id'=>$param['chapter_id'],
            'money' =>$param['money']
        ];
        db("member")->where('id',$m_id)->setDec('money',$money);
        $res = db('buy')->insert($data);
        if($res){
            return ['state' => 200,'msg' => 'SUCCESS'];
        }else{
            return ['state' => -1,'msg' => '系统错误 请刷新后再试'];
        }
    }
    // 章节点击量
    public function chapter_pv($chapter,$book)
    {
        $ip = request()->ip();
        $key = $ip.':'.$chapter['id'];
        $redis = new Redis();
        $val = $redis->get($key);
        if(!$val){
            $redis->set($key,$chapter['id'],300);
            db('chapter')->where('id',$chapter['id'])->update(['pv'=>$chapter['pv']+1]);
            db('book')->where('id',$book['id'])->update(['chapter_pv' => $book['chapter_pv']+1]);
        }
        /*$pv = db('pv_log')->where('ip',$ip)->where('chapter_id',$chapter['id'])->find();
        if(empty($pv)){
            db('chapter')->where('id',$chapter['id'])->update(['pv'=>$chapter['pv']+1]);
            db('pv_log')->insert(['ip' => $ip,'chapter_id'=>$chapter['id'],'up_time' => time(),'book_id' => 0]);
        }else{
            if($time - $pv['up_time'] > 60){
                db('chapter')->where('id',$chapter['id'])->update(['pv'=>$chapter['pv']+1]);
                db('pv_log')->where('ip',$ip)->where('chapter_id',$chapter['id'])->update(['up_time' => time(),'pv' => $pv['pv']+1]);
            }
        }*/
    }

}